<?php
/*
 * CreateAdmin Class
 *
 * This action creates Administrator user account.  
 */
class CreateAdmin extends SQLAction {
    
    var $admin_user;
    var $cypherPass;
    var $admin_pass;
    
   /* function CreateAdmin($params) {
        parent::SQLAction($params);
        
        $this->interactive = false;
        $this->grouping = false;
    }*/
    
    /*
     * This function needs to be overriden in the implementing class
     * and should return either TRUE or FALSE.
     * 
     * @var $params array Array of parameters needed for the specific implementation
     */
    function perform() {
        if (empty($this->loop)) {
            $this->loop = 1;
        }

        if($this->prepareParameters() === FALSE){
            $this->result = INSTALLER_ACTION_FAIL;
            return $this->result;
        }

	if($this->script == 'No'){
			$this->result = INSTALLER_ACTION_SUCCESS;
			return $this->result;
	}

        # Connect to the DB
        $db = $this->connect();
        if ($db === FALSE)
            return $this->result;
        //require_once(APP_PATH.'/aes.php');
        require_once(APP_PATH.'/AES.class.php');
        require_once('/var/SINAPSIS_CONFIG.php');
        $password=$this->admin_pass;
        $md5password=md5($password);
        
        //var_dump($password);
        //var_dump($md5password);
        //var_dump($cypherPass);
        $AES=new AES($cypherPass);
	//$aespassword=AESEncryptCtr($md5password, $cypherPass, 256);
        $aespassword=$AES->encrypt($md5password);
        $aes=new AES($cypherPass);
        $deaespassword=$aes->decrypt($aespassword);
	$aespassword=base64_encode($aespassword);
        
        //$deaespassword=AESDecryptCtr($aespassword, $cypherPass,256);
		
        $sql = "INSERT INTO `care_users` (`name`, `login_id`, `password`, `permission`, `exc`, `modify_id`, `create_id`) VALUES ('admin', '".$this->admin_user."', '".$aespassword."', 'System_Admin', 1, 'auto-installer', 'auto-installer')";
        
        $ok = $db->Execute($sql);
        if ($ok) {
            $this->result = INSTALLER_ACTION_SUCCESS;
            $this->result_message = "Usuario Administrador Creado Exitosamente";
            $this->loop = 3;
        } else {
            $this->result = INSTALLER_ACTION_FAIL;
            $this->result_message = "No se Puede Crear Usuario Administrador: ".$db->ErrorMsg();
            $this->loop = 2;
        }
        
        return $this->result;
    }
        
    function prepareParameters() {
        if ($this->prepareDBParameters() === FALSE)
            return FALSE;

        $engine =& $GLOBALS['INSTALLER']['ENGINE'];

        if (isset($this->params['adminuser_field'])) {
            $adminuser_field = $engine->getField($this->params['adminuser_field']);
            $this->admin_user = $adminuser_field->value;
        } else if (isset($this->params['adminuser'])) {
            $this->admin_user = $this->params['adminuser'];
        } else {
            $this->result_message = "Could not determine Administrator username, please provide a adminuser_field or adminuser parameter!";
            return FALSE;
        }

        if (isset($this->params['Cypher'])) {
            $cypher = $engine->getField($this->params['Cypher']);
            $this->cypherPass = $cypher->value;
        } else if (isset($this->params['cypherPass'])) {
            $this->cypherPass = $this->params['cypherPass'];
        } else {
            $this->result_message = "Could not determine cypher password, please provide a cypher or a cypher Pass parameter!";
            return FALSE;
        }

        if (isset($this->params['adminpass_field'])) {
            $adminpass_field = $engine->getField($this->params['adminpass_field']);
            $this->admin_pass = $adminpass_field->value;
        } else if (isset($this->params['adminpass'])) {
            $this->admin_pass = $this->params['adminpass'];
        } else {
            $this->result_message = "Could not determine Administrator password, please provide a adminpass_field or adminpass parameter!";
            return FALSE;
        }
    }
    
}
?>
